<?php
/**
 * 默认控制器
 * @author Administrator
 *
 */
class main extends Controller{
    function main(){
        parent::Controller();
    }
    /**
     * 首页
     */
    function index(){
        if (!isset($_SESSION['DLRID']) || $_SESSION['DLRNAME'] == ""){
            redirect(printUrl('Main', 'login'));
        }else{
            redirect(printUrl('HomePage', 'index'));
        }
    }
    /**
     * 登陆界面
     */
    function login(){
        if ($_POST){
            $accout = trim($this->input->post('accout'));
            $password = trim($this->input->post('password'));
            $isRemember = (int)$this->input->post('isRemember');
            $accout || showError('请输入账号');
            ereg("^[a-zA-Z0-9_-]{2,50}$", $accout) || showError('请输入正确的账号');
            $password || showError('请输入正确的密码');
            //当前时间
            $nowTime = time();
            //载入模型
            $this->load->model('passwordSetupModel');
            $this->load->model('pvloginLockModel');
            $this->load->model('pvloginLogModel');
            $this->load->model('sellpointModel');
            //登录次数过多限制
            $passSetupData = $this->passwordSetupModel->getData(array('type'=>5,'enable'=>1));
            if ($passSetupData){
                $lockData = $this->pvloginLockModel->getData(array('Account'=>$accout,'unlocktime >' => $nowTime));
                if ($lockData){
                    showError('你的账号密码错误次数过多已被锁定，24小时内不能登录');
                }
            }
            //验证账号是否正确
            $sellpointData = $this->sellpointModel->getData(array('Account'=>$accout));
            if (!$sellpointData){//没有专营店数据
                showError('账号或密码不正确');
            }elseif ($sellpointData['Password'] != $password){//密码不正确
                if ($passSetupData['enable'] == 1){
                    $ip = $_SERVER['REMOTE_ADDR'];
                    $data = array(
                        'SellPointID' => $sellpointData['SellPointID'],
                        'Account' => $accout,
                        'ip' => $ip,
                        'createtime' => $nowTime,
                    );
                    
                    $this->pvloginLogModel->add($data);
                    
                    $logCount = $this->pvloginLogModel->getCount(array('Account'=>$accout,'createtime > ' => strtotime("-1 days")));
                    if ($logCount >= $passSetupData['rule']){
                        $data = array(
                            'SellPointID' => $sellpointData['SellPointID'],
                            'Account' => $accout,
                            'freezetime' => $nowTime,
                            'unlocktime' => strtotime("+1 days"),
                        );
                        //插入锁定日志
                        $this->pvloginLockModel->add($data);
                        //删除登录日志
                        $this->pvloginLogModel->del(array('SellPointID'=>$sellpointData['SellPointID']), '*');
                    }
                }
                showError('账号或密码不正确');
            }else{//密码账号正确
                //删除登录日志
                $this->pvloginLogModel->del(array('SellPointID'=>$sellpointData['SellPointID']), '*');
                $passSetupData = $this->passwordSetupModel->getData(array('type'=>2,'enable'=>1));
                if ($passSetupData){
                    $this->load->model('passwordSplogModel');
                    $log = $this->passwordSplogModel->getList(array('SellPointID'=>$sellpointData['SellPointID']),'','','','createtime DESC');
                    if (!$log){
                        $data = array(
                            'SellPointID' => $sellpointData['SellPointID'],
                            'Password' => $password,
                            'createtime' => $nowTime,
                        );
                        $this->passwordSplogModel->add($data);
                    }
                    $log = array_values($log);
                    if ($nowTime - $log[0]['createtime'] > $passSetupData['rule'] * 24 * 3600){
                        showError("你的密码已超过".$passSetupData['rule']."天没有修改，请修改后再登录", 'password_manage/change_psw.php');
                    }
                }
                //判断账号是否已停用
                if ($sellpointData['isOpen'] == 2){
                    showError('该账号已停用');
                }
                //更新登录时间
                $this->sellpointModel->edit(array('SellPointID'=>$sellpointData['SellPointID']), array('logtime'=>date("Y-m-d H:i:s", $nowTime)));
                //保存session
                $this->load->library('session');
                if ($isRemember){
                    setcookie("lAccount",$accout,time()+500000000);
                    setcookie("lPassword",$password,time()+500000000);
                }else{//2009-3-27 帐号密码写入cookie，以便线下活动管理平台验证
                    setcookie("lAccount",$accout);
                    setcookie("lPassword",$password);
                }
                $_SESSION['DLRACCOUNT'] = $sellpointData["Account"];
                $_SESSION['DLRID'] = $sellpointData["SellPointID"];
                $_SESSION['DLRCODE'] = $sellpointData["Coding"];
                $_SESSION['DLRNAME'] = $sellpointData["SellPointName"];
                $_SESSION['DLRSTATE'] = $sellpointData["SpState"];
                $_SESSION['DLRSTATE_SYS'] = $sellpointData["SpState_sys"];
                $_SESSION['Region']	=	$sellpointData["Region"];
                $_SESSION['Region_2014']	=	$sellpointData["Region_2014"];
                $_SESSION['qicheng_SpSArea']	=	$sellpointData["qicheng_SpSArea"];
                $_SESSION['AREA'] = $sellpointData["SpArea"];
                $_SESSION['TmpLevel'] = $sellpointData['TmpLevel'];
                $_SESSION['BAREA'] = $sellpointData["Area"];
                $_SESSION['BAREA_2014'] = $sellpointData["Area_2014"];
                $_SESSION['SPSAREA'] = $sellpointData["SpSArea"];
                $_SESSION['SPSAREA_2014'] = $sellpointData["SpSArea_2014"];
                
                $_SESSION['BAREA_2015'] = $sellpointData["Area_2015"];
                $_SESSION['Region_2015']	=	$sellpointData["Region_2015"];
                $_SESSION['SPSAREA_2015'] = $sellpointData["SpSArea_2015"];
                
                ///
                $_SESSION['PROVINCE'] = $sellpointData["Province"];
                $_SESSION['COUNTY'] = $sellpointData["County"];
                $_SESSION['ADNWPermission'] = $sellpointData["ADNWPermission"];
                $_SESSION['FundPermission'] = $sellpointData["FundPermission"];
                $_SESSION['isAdminManager'] = $sellpointData["isAdminManager"];
                if($_SESSION['DLRSTATE_SYS']=="101" or $_SESSION['DLRSTATE_SYS']=="11")
                {
                    $_SESSION['isQiCheng'] = 1;
                }else{
                    $_SESSION['isQiCheng'] = 0;
                }
                if($_SESSION['DLRSTATE_SYS']=="101" or $_SESSION['DLRSTATE_SYS']=="11")
                {
                    $jumpurl = printUrl('HomePage', 'index',array('brand'=>2));
                }elseif($_SESSION['DLRSTATE_SYS']=="102"){
                    $jumpurl = "choose.php";
                }else{
                    $jumpurl = printUrl('HomePage', 'index',array('brand'=>1));
                }
                showSuccess('登录成功', $jumpurl);
            }
        }else{
            if (!isset($_SESSION['DLRID']) || $_SESSION['DLRNAME'] == ""){
            }else{
                redirect(printUrl('HomePage', 'index'));
            }
            if($_COOKIE['lAccount']==""){
                $this->viewData['account'] = "输入账号";
                $this->viewData['password'] = "输入密码";
                $this->viewData['isRemember'] = false;
            }else{
                $this->viewData['account'] = $_COOKIE['lAccount'];
                $this->viewData['password'] = $_COOKIE['lPassword'];
                $this->viewData['isRemember'] = true;
            }
            $this->load->view('Main/login', $this->viewData);
        }
    }
    /**
     * 退出登录
     */
    function quit(){
        session_start();
        setcookie("lAccount","");
        setcookie("lPassword","");
        setcookie("dflpvmkt","");
        setcookie("rmp2015_dflpvmkt_session","");
        setcookie("rmp_dflpvmkt_session","");
        setcookie("qirmp_dflpvmkt_session","");
        
        unset($_COOKIE["lAccount"]);
        unset($_COOKIE["lPassword"]);
        unset($_SESSION['DLRACCOUNT']);
        unset($_SESSION['DLRID']);
        unset($_SESSION['DLRCODE']);
        unset($_SESSION['DLRNAME']);
        unset($_SESSION['DLRSTATE']);
        unset($_SESSION['AREA']);
        unset($_SESSION['PROVINCE']);
        unset($_SESSION['COUNTY']);
        unset($_SESSION['ADNWPermission']);
        session_destroy();
        redirect(printUrl('Main', 'login'));
    }
}